package ${package_name}.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshb.upms.core.constants.NumConstant;
import ${package_name}.core.exception.BusinessException;
import ${package_name}.core.exception.OptimisticLockException;
import ${package_name}.entity.${table_entity.entityName};
import ${package_name}.mapper.${table_entity.entityName}Mapper;
import ${package_name}.service.${table_entity.entityName}Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ${table_entity.entityName}ServiceImpl extends ServiceImpl<${table_entity.entityName}Mapper, ${table_entity.entityName}> implements ${table_entity.entityName}Service {

    @Resource
    private ${table_entity.entityName}Mapper ${table_entity.aliasName}Mapper;

    @Override
    public Page<${table_entity.entityName}> findPage(int pageNo, int pageSize, ${table_entity.entityName} condition) {

        Page<${table_entity.entityName}> page = new Page<>(pageNo, pageSize, true);
        page.setRecords(findList(page, condition));
        return page;
    }

    @Override
    public List<${table_entity.entityName}> findList(Page<${table_entity.entityName}> page, ${table_entity.entityName} condition) {
        return this.${table_entity.aliasName}Mapper.findList(page, condition);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add${table_entity.entityName}(${table_entity.entityName} ${table_entity.aliasName}) {
        this.${table_entity.aliasName}Mapper.insert(${table_entity.aliasName});
        return ${table_entity.aliasName}.getId();
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del${table_entity.entityName}(${table_entity.entityName} ${table_entity.aliasName}) {
        ${table_entity.entityName} delEntity = new ${table_entity.entityName}();
        delEntity.setId(${table_entity.aliasName}.getId());
        delEntity.setVersion(${table_entity.aliasName}.getVersion());
        delEntity.setDeleted(NumConstant.ONE);
        update${table_entity.entityName}WithLock(delEntity);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update${table_entity.entityName}WithLock(${table_entity.entityName} ${table_entity.aliasName}) {

        if (null == ${table_entity.aliasName}.getVersion()) {
            throw new BusinessException("版本不能为空");
        }

        if (!this.updateById(${table_entity.aliasName})) {
            throw new OptimisticLockException("数据已被修改");
        }
    }
}
